package com.powermanager.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.powermanager.R;
import com.powermanager.bean.LineBean;
import com.powermanager.bean.TimeBean;

import java.util.List;

/**
 * Created by lqy on 16/7/5.
 */
public class TimerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<TimeBean> lists;
    private OnItemClickeListenenr onItemClickeListenenr;

    public TimerAdapter(Context context, List<TimeBean> lists) {
        this.context  = context;
        this.lists = lists;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_timer, parent, false));
        return myViewHolder;
    }

    public void setOnItemClickeListenenr(OnItemClickeListenenr onItemClickeListenenr) {
        this.onItemClickeListenenr = onItemClickeListenenr;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        final TimeBean timeBean = lists.get(position);
        myViewHolder.mTvName.setText(timeBean.getLine().getName());
        myViewHolder.mTvTime.setText(timeBean.getOnTime()+"--"+timeBean.getOffTime());
        String[] h_m_s_start  = timeBean.getOnTime().split("/");
        final String h_start = h_m_s_start[0];
        final String min_start = h_m_s_start[1];
        String[] h_m_s_end = timeBean.getOffTime().split("/");
        final String h_end = h_m_s_end[0];
        final String min_end = h_m_s_end[1];
        myViewHolder.mBtnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (onItemClickeListenenr!=null){
                    onItemClickeListenenr.onItemClicked(timeBean.getLine(),isChecked,h_start,min_start,h_end,min_end);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTvName;
        TextView mTvTime;
        ToggleButton mBtnToggle;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTvName = (TextView) itemView.findViewById(R.id.item_timer_tv_name);
            mTvTime = (TextView) itemView.findViewById(R.id.item_timer_tv_time);
            mBtnToggle = (ToggleButton) itemView.findViewById(R.id.item_timer_btn_toggle);
        }
    }
    public interface  OnItemClickeListenenr{
        public void onItemClicked(LineBean lineBean, boolean isChecked, String starthour, String startminute, String endHour, String endMinute);
    }
}
